<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="box">

    </div>

    <script src="./js/react18.js"></script>
    <script src="./js/react-dom18.js"></script>
    <script src="./js/babel.min.js"></script>
    <script src="./js/prop-types.js"> </script>

    <script type="text/babel">

        // 创建一个根对象：
        let root = ReactDOM.createRoot(document.getElementById("box"));

        class Son extends React.Component{
            sonFn01(){
                console.log("sonFn01");
            }
            render=()=>(
                <div>子组件</div>
            )
        }


        class MyPerson extends React.Component {

            constructor(props) {
                super();
                this.sonRef=null;
                this.sonRef02 = React.createRef();//相当于vue组合式api中 ref对象。
            }

            fn01=()=>{
                this.refs.username.value="范旭康";
            }
            
            fn02=()=>{
                this.refs.sonRef.sonFn01();
            }

            fn03=()=>{
                this.sonRef.sonFn01();
            }

            fn04=()=>{
                this.sonRef02.current.sonFn01();
            }
            
            render() {
                return (
                    <div>
                        <h1>状态机</h1>
                        {/*1、给ref属性赋值为字符串*/}
                        <input type="text" ref="username" />
                        <input type="button" value="获取官方标签" onClick={this.fn01} />
                        <Son ref="sonRef" />
                        <input type="button" value="获取自定义组件01" onClick={this.fn02} />
                        {/*2、给ref属性赋值为回调函数，回调函数的参数就是组件对象本身
                              
                        */}
                        <Son ref={(el)=>{
                            console.log("el",el);
                            this.sonRef = el;//把el赋给类的属性，其它方法就可以使用了。
                        }} />
                        <input type="button" value="获取自定义组件02" onClick={this.fn03} />
                        {/*
                            3、使用React.createRef()
                        */}
                        <Son ref={this.sonRef02} />
                        <input type="button" value="获取自定义组件03" onClick={this.fn04} />
                    </div>
                )
            }
        }

        const jsx = (
            <div>
                <MyPerson></MyPerson>
            </div>)


        // 把jsx渲染到根对象对应的dom容器里
        root.render(jsx);


    </script>
</body>

</html>